<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
<?php
    include 'header.php';
  ?>
<body>


<?php
$username = "fossilplot";
$password = "fossilplot";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MySQL");


//select a database to work with
$selected = mysql_select_db("fossilplot",$dbhandle)
  or die("Could not select examples");

$debug=0;
$period=$_POST['Periods'];
$level=$_POST['level'];
//echo "period is $period<br>";
//echo "level is $level<br>";

$pd_short=mysql_query("select pd_short from period where pd='$period'") or die ("error selecting from stgintv!!");
while($row_pd_short=mysql_fetch_array($pd_short)){
    $short=$row_pd_short{'pd_short'};
    //echo "short is ".$row_pd_short{'pd_short'}."<br>";

}

$phylum=mysql_query("select phylum_name from phylum") or die ("error selecting from stgintv!!");
$i=0;
while($row_phy=mysql_fetch_array($phylum)){
    $phylum_arr[$i][0]=$row_phy{'phylum_name'};
    $phylum_arr[$i][1]=0;
    $i++;
}
//echo "i is $i, length is ".sizeof($phylum_arr);
//echo "<br>#3 is ".$phylum_arr[26][0]."<br>";


if($short=="Pg"){
    $genera=mysql_query("select phylum_name, fad_period, lad_period from genus where (fad_period='T' and fad_date >='23.03') or (lad_period='T' and lad_date >='23.03')") or die ("error selecting from period!!");
}
else if($short =="Ng"){
        $genera=mysql_query("select phylum_name, fad_period, lad_period from genus where (fad_period='T' and fad_date <'23.03') or (lad_period='T' and lad_date <'23.03')") or die ("error selecting from period!!");

}
else if($short == "VC"){
        //echo "it is true!";
        $genera=mysql_query("select phylum_name, fad_period, lad_period from genus where fad_period='V' or fad_period ='Cm' or lad_period='V' or lad_period ='Cm'") or die ("error selecting from period!!");

}

else{
$genera=mysql_query("select phylum_name, fad_period, lad_period from genus where fad_period='$short' or lad_period='$short'") or die ("error selecting from stgintv!!");
}
while($row_genera=mysql_fetch_array($genera)){
     //echo "genera is ".$row_genera{'phylum_name'}."<br>";
      //if($row_genera{'fad_period'}==$short || $row_genera{'lad_period'}==$short ||(($short=="Pg" || $short=="Ng")&& ($row_genera{'fad_period'} =="T" ||$row_genera{'lad_period'} =="T")) || ($short =="VC" &&($row_genera{'fad_period'}=="Cm" ||$row_genera{'fad_period'}=="V" ||$row_genera{'lad_period'}=="Cm" ||$row_genera{'lad_period'}=="V")))
      //{
        for($i=0; $i<sizeof($phylum_arr); $i++) 
            if($phylum_arr[$i][0] ==$row_genera{'phylum_name'})
                $phylum_arr[$i][1]++;
        
      //}
}

//for($i=0; $i<sizeof($phylum_arr); $i++) 
    //echo "arr $i is".$phylum_arr[$i][1]."<br>";



$File = "./periodgra.php"; 
  $Handle = fopen($File, 'w');
    $Data = "<?php
include (\"./src/jpgraph.php\");
include (\"./src/jpgraph_bar.php\");
\$n = 27;
	for(\$i=0; \$i < \$n; ++\$i ) {
	    \$tmp =\$phylum_arr[\$i][0];
	    \$targ1[\$i] = \"http://www.fossilplot.org/test/new/v9/graph/getgenera.php?arg1=\$tmp&arg2=\$period\";	    
	    \$alts1[\$i] = \"number of genera=%d\";   
	}


\$datay=array";
fwrite($Handle, $Data);
$tmp =$phylum_arr[0][1];
$barval="($tmp";
for($i=1; $i<sizeof($phylum_arr); $i++){
    $barval =$barval.",".$phylum_arr[$i][1];
}

$barval=$barval.")";

$Data="$barval;
\$datax=array";
fwrite($Handle, $Data);


$tmp =$phylum_arr[0][0];
$bar_x="(\"$tmp\"";
for($i=1; $i<sizeof($phylum_arr); $i++){
    if($phylum_arr[$i][0] =="Ph. LOBOPODA (= Onychophora)"){ //there is a special phylum which is to long to be cut
        $bar_x =$bar_x.","."\""."Ph. LOBOPODA"."\"";
    }
    else{
      $bar_x =$bar_x.","."\"".$phylum_arr[$i][0]."\"";
    }
}

$bar_x=$bar_x.")";
$Data="$bar_x;

//\$datay=array(12,8,19,3,10,5);
//\$datax=array(\"ph1\",\"ph2\",\"ph3\",\"ph4\",\"ph5\",\"ph6\");

\$graph = new Graph(800,450,\"auto\");    
\$graph->SetScale(\"textlin\");
\$graph->SetShadow();
\$graph->img->SetMargin(40,30,20,130);
\$bplot = new BarPlot(\$datay);

\$bplot->SetFillColor('green');
\$bplot->value->Show();
\$graph->Add(\$bplot);

\$graph->title->Set(\"$period period graph\");
\$graph->xaxis->title->Set(\"Phylum Names\");
\$graph->yaxis->title->Set(\"Number of Genera\");
\$graph->xaxis->SetTickLabels(\$datax);
\$graph->xaxis->SetLabelAngle(90);
\$graph->xaxis->title->SetMargin(90);
\$graph->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD,4);
\$bplot->SetCSIMTargets(\$targ1,\$alts1);
//\$graph->Stroke();

\$graph->Stroke('./jpgraph_cache/periodgra.png');
\$imgmap = \$graph->GetHTMLImageMap('barmap');
echo \$imgmap;
echo \"<img src='http://www.fossilplot.org/test/graph/jpgraph_cache/periodgra.png' ISMAP USEMAP='#barmap' border='0'>\";

//echo \"<img src='http://localhost/test/graph/jpgraph_cache/periodgra.png' ISMAP USEMAP='#barmap' border='0'>\";
?>
    
    
    ";
    fwrite($Handle, $Data);
    
    $myFile = "./jpgraph_cache/periodgra.png";
    if(file_exists($myFile)){
    unlink($myFile);
    }
 //echo "<img src=\"http://localhost/test/graph/periodgra.php\">"; 
 require('periodgra.php');

?>

<?php include 'footer.php';?>

</body>
</html>
